using System.Windows;
using Gavel.Common.Component.Helper;
using Gavel.Common.Utilities.Model;
using Gavel.ModLink.Helper;
using Gavel.ModLink.ViewModel;

namespace Gavel.ModLink.Views;

/// <summary>
/// 地址配置页
/// </summary>
public partial class AddressView
{
    /// <summary>
    /// 地址配置视图模型
    /// </summary>
    private readonly AddressViewModel _viewModel;

    /// <summary>
    /// 默认构造
    /// </summary>
    public AddressView()
    {
        InitializeComponent();
        _viewModel = (AddressViewModel)DataContext;
        ClientHelper.Instance.SocketStatusChangedEvent += status => _viewModel.Status = status;
    }

    /// <summary>
    /// 连接套接字
    /// </summary>
    /// <param name="sender">事件发送者</param>
    /// <param name="e">事件参数</param>
    private async void ConnectSocket(object sender, RoutedEventArgs e)
    {
        try
        {
            await ClientHelper.Instance.Connect(new Connection(_viewModel.Host, _viewModel.Port));
        }
        catch (Exception exception)
        {
            NotifyHelper.Instance.Error($"客户端连接异常\n{exception.Message}");
        }
    }

    /// <summary>
    /// 套接字断开连接
    /// </summary>
    /// <param name="sender">事件发送者</param>
    /// <param name="e">事件参数</param>
    private async void DisconnectSocket(object sender, RoutedEventArgs e)
    {
        try
        {
            await ClientHelper.Instance.Disconnect();
        }
        catch (Exception exception)
        {
            NotifyHelper.Instance.Error($"客户端连接异常\n{exception.Message}");
        }
    }
}